﻿package controllers.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import rbac.models.Oplog;
import rbac.models.User;

import utils.PinyingUtil;

import models.Zjlist;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;

public class ZjlistController extends ApplicationController {

	public void index() throws Exception {

	}

	public View getAllZjlistItems(String q, String t, String sort, String order)
			throws Exception {
		
		String orderBy = "id";
		if (sort != null && order != null) {
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();

		/**
		 * if (t != 0){ cond += " and (parentId = ?)"; tmpArgs.add(t); }
		 * 
		 * if (q != null){ cond +=
		 * " and (name like '%"+q+"%' or memo like '%"+q+"%' ) "; }
		 **/
		if (q != null) {
			cond = cond + " and (sxname like '%" + q + "%' ) ";
		}
		
		if (t != null) {
			cond += " and (mainid = '" + t + "' or company_id = '" + t + "')";
		}

		/**if (q1 != null) {
			cond = cond + " and (mc like '%" + q1 + "%'  or xm like '%" + q1
					+ "%' or dz1 like '%" + q1 + "%' ) ";
		}**/

		Object[] args = tmpArgs.toArray();

		long total = Zjlist.count(Zjlist.class, cond, args);
		List<Zjlist> items = null;
		if (q != null) {
		  items = Zjlist.findAll(Zjlist.class, cond, args, orderBy, 20);
		}else{
		  items = Zjlist.findAll(Zjlist.class, cond, args, orderBy);
		}

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", items);

		return new JsonView(result);
	}
	
	public View getZjlistLeftItems(int page, int rows,String q, String t, String sort, String order)
			throws Exception {
		if (page == 0) page = 1;
		if (rows == 0) rows = 50;
		String orderBy = "id";
		if (sort != null && order != null) {
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();

		/**
		 * if (t != 0){ cond += " and (parentId = ?)"; tmpArgs.add(t); }
		 * 
		 * if (q != null){ cond +=
		 * " and (name like '%"+q+"%' or memo like '%"+q+"%' ) "; }
		 **/
		if (t != null) {
			cond += " and (mainid = '" + t + "' or company_id = '" + t + "')";
		}

		if (q != null) {
			cond = cond + " and (mc like '%" + q + "%'  or xm like '%" + q
					+ "%' or dz1 like '%" + q + "%' ) ";
		}

		Object[] args = tmpArgs.toArray();

		long total = Zjlist.count(Zjlist.class, cond, args);
		List<Zjlist> items = Zjlist.findAll(Zjlist.class, cond, args, orderBy,rows, (page - 1) * rows);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", items);

		return new JsonView(result);
	}

	public View getZjlistItems(int page, int rows, String q, String t,
			String sort, String order) throws Exception {
		String orderBy = "id";
		if (sort != null && order != null) {
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();

		/**
		 * if (t != 0){ cond += " and (parentId = ?)"; tmpArgs.add(t); }
		 * 
		 * if (q != null){ cond +=
		 * " and (name like '%"+q+"%' or memo like '%"+q+"%' ) "; }
		 **/
		if (t != null) {
			cond += " and (mainid = '" + t + "' or company_id = '" + t + "')";
		}

		if (q != null) {
			cond = cond + " and (mc like '%" + q + "%'  or xm like '%" + q
					+ "%' or dz1 like '%" + q + "%' ) ";
		}

		Object[] args = tmpArgs.toArray();

		long total = Zjlist.count(Zjlist.class, cond, args);
		List<Zjlist> items = Zjlist.findAll(Zjlist.class, cond, args, orderBy,
				rows, (page - 1) * rows);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", items);

		return new JsonView(result);
	}

	public View saveZjlist(Zjlist item, String[] rids) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		// 其他需要系统自动填写的字段
		// 其他操作如写入日志
		String py = PinyingUtil.getHeadByStrings(item.getMc());
		int pylen = py.length();
		if (pylen > 10) {
			pylen = 10;
			py = py.substring(0, 10);
		}
		item.setXh(py + genRandomNum(20 - pylen));
		if (item.save()) {
			User user = (User) rbac.getCurrentUser();				
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建单位，内容为：" + new JsonView(item));									

			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}

		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View updateZjlist(int id, Map<String, Object> params, String[] rids)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		Zjlist item = Zjlist.find(Zjlist.class, id);
		JsonView olditem= new JsonView(item);
		item = Zjlist.updateModel(item, params);

		if ((item.getXh() == null) || (item.getXh().equals(""))) {

			String py = PinyingUtil.getHeadByStrings(item.getMc());
			int pylen = py.length();
			if (pylen > 10) {
				pylen = 10;
				py = py.substring(0, 10);
			}
			item.setXh(py + genRandomNum(20 - pylen));
		}

		// 其他需要系统自动填写的字段
		// 其他操作如写入日志
		if (item.save()) {
			User user = (User) rbac.getCurrentUser();				
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改单位，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										

			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}

		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View destroyZjlist(int id) throws Exception {
		Zjlist item = Zjlist.find(Zjlist.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除单位，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}

	private String genRandomNum(int len) {
		String tmp = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		int count = 0;
		while (count < len) {
			int v = Math.abs(r.nextInt(36));
			if (v < tmp.length()) {
				String s = tmp.substring(v, v + 1);
				pwd.append(s);
				count++;
			}
		}
		return pwd.toString();
	}
}
